#!/usr/bin/python3
"""
Configure basic aspects of the anaconda installer

Create an anaconda configuration file `90-osbuild.conf` in
the folder `/etc/anaconda/conf.d` to configure anaconda.

Currently only the list of enabled kickstart modules is
configurable via the `kickstart-modules` option.
"""

import os
import sys


import osbuild.api


SCHEMA = """
"additionalProperties": true,
"required": ["kickstart-modules"],
"properties": {
  "kickstart-modules": {
    "type": "array",
    "description": "Kick start modules to enable",
    "items": {
      "type": "string"
    },
    "minItems": 1
  }
}
"""

CONFIG = """
# osbuild customizations

[Anaconda]
# List of enabled Anaconda DBus modules
kickstart_modules =
"""


def main(tree, options):
    modules = options["kickstart-modules"]
    product_dir = os.path.join(tree, "etc/anaconda/conf.d")
    os.makedirs(product_dir, exist_ok=True)

    with open(os.path.join(product_dir, "90-osbuild.conf"), "w") as f:
        f.write(CONFIG)
        for m in modules:
            f.write(f"    {m}\n")


if __name__ == '__main__':
    stage_args = osbuild.api.arguments()
    r = main(stage_args["tree"],
             stage_args["options"])
    sys.exit(r)
